package com.ccsoft.annoreflect.utils;

import com.ccsoft.annoreflect.anno.Table;
import com.ccsoft.annoreflect.anno.TableField;
import org.apache.ibatis.jdbc.SQL;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * @author chanchaw
 * @create 2022-09-24 10:36
 */
public class MySqlProvider {

    public String getQueryListSQL(Map<String,Object> params){
        Class<?> modelClass = (Class<?>)params.get("modelClass");
        Table tableAnnotation = modelClass.getAnnotation(Table.class);
        if(tableAnnotation == null ) throw new RuntimeException("没有为实体类使用注解@Table");
        String tableName = tableAnnotation.tableName();
        if(tableName == null || tableName.length()<=0) throw new RuntimeException("注解@Table没有设置表名");

        SQL sql = new SQL();

        Field[] declaredFields = modelClass.getDeclaredFields();
        for(Field field:declaredFields){
            TableField fieldAnnotation = field.getAnnotation(TableField.class);
            if(fieldAnnotation == null) continue;

            String fieldName = fieldAnnotation.fieldName();
            if(fieldName == null || fieldName.length()<=0) continue;

            sql.WHERE(fieldName+"=#{requestData."+field.getName()+"}");
        }

        String selectSql = sql.SELECT("*").FROM(tableName).toString();
        System.out.println("查询SQL：" + selectSql);
        return selectSql;
    }

    public String getUpdateSQL(Map<String,Object> params){
        Class<?> modelClass = (Class<?>)params.get("modelClass");
        Table tableAnnotation = modelClass.getAnnotation(Table.class);
        if(tableAnnotation == null ) throw new RuntimeException("没有为实体类使用注解@Table");
        String tableName = tableAnnotation.tableName();
        if(tableName == null || tableName.length()<=0) throw new RuntimeException("注解@Table没有设置表名");

        SQL sql = new SQL();

        Field[] declaredFields = modelClass.getDeclaredFields();
        for(Field field:declaredFields){
            TableField fieldAnnotation = field.getAnnotation(TableField.class);
            if(fieldAnnotation == null) continue;

            String fieldName = fieldAnnotation.fieldName();
            if(fieldName == null || fieldName.length()<=0) continue;

            Boolean isPK = fieldAnnotation.isPK();
            if(isPK == true)
                sql.WHERE(fieldName+"=#{requestData."+field.getName()+"}");
            else
                sql.SET(fieldName + "=#{requestData." + field.getName() + "}");
        }

        return sql.UPDATE(tableName).toString();
    }

    public String getDeleteSQL(Map<String,Object> params){
        Map<String,Object> dataMap = (Map<String,Object>)params.get("requestData");
        Class<?> modelClass = (Class<?>)params.get("modelClass");

        Table tableAnno = modelClass.getAnnotation(Table.class);
        String tableName = tableAnno.tableName();
        SQL sql = new SQL();// 借助 mybatis 的 SQL 生成 sql 语句

        Field[] fields = modelClass.getDeclaredFields();
        for(Field field:fields){
            TableField fieldAnno = field.getAnnotation(TableField.class);
            if(fieldAnno == null) continue;
            Boolean isPK = fieldAnno.isPK();
            if(isPK == true) {
                sql.WHERE(fieldAnno.fieldName() + "=#{requestData." + field.getName() + "}");
                break;
            }
        }

        String ret = sql.DELETE_FROM(tableName).toString();
        System.out.println("执行删除的SQL：" + ret);
        return ret;
    }
}
